package com.olc.railing.utils;

import com.olc.railing.constant.Constants;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogDumper extends Thread {

    private Process logcatProc;
    private BufferedReader mReader = null;
    private boolean mRunning = true;
    String cmds = null;
    private String mPID;
    private FileOutputStream out = null;

    public LogDumper(String pid, String dir) {
        mPID = pid;
        try {
            File file = new File(dir,  getFileName() + ".txt");
            if (!file.exists()) {
                file.createNewFile();
                MyLog.e(MyLog.TAG,"LogDumper 创建文件夹");
            }
            out = new FileOutputStream(file,true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        /**
         *
         * 日志等级：*:v , *:d , *:w , *:e , *:f , *:s
         *
         * 显示当前mPID程序的 E和W等级的日志.
         *
         * */

       //  cmds = "logcat *:e *:w | grep \"(" + mPID + ")\"";
       //  cmds = "logcat  | grep \"(" + mPID + ")\"";//打印所有日志信息
         cmds = "logcat -s railing_log";//打印标签过滤信息
    //    cmds = "logcat -v obu_log";

    }

    public void stopLogs() {
        mRunning = false;
    }

    @Override
    public void run() {
        try {
           // MyLog.e(MyLog.TAG,"LogDumper run");
            logcatProc = Runtime.getRuntime().exec(cmds);
            mReader = new BufferedReader(new InputStreamReader(
                    logcatProc.getInputStream()), 1024);
            String line = null;
            while (mRunning && (line = mReader.readLine()) != null) {
                if (!mRunning) {
                    break;
                }
                if (line.length() == 0) {
                    continue;
                }
                if (out != null && line.contains(mPID)) {
                    out.write((line + "\n").getBytes());
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (logcatProc != null) {
                logcatProc.destroy();
                logcatProc = null;
            }
            if (mReader != null) {
                try {
                    mReader.close();
                    mReader = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out = null;
            }

        }

    }
    public String getFileName() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date(System.currentTimeMillis()))+"_"+CommUtils.getDeviceSN();
        MyLog.e(MyLog.TAG,"getFileName date "+date);
        return date;// 2012年10月03日 23:41:31
    }

}